package com.exmaple.dynamicapp.injection;

import android.app.Activity;
import android.util.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * <p>
 * 事件调用
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/8/11
 */
public class EventInvocationHandler implements InvocationHandler {
    private static final String TAG = "EventInvocationHandler";
    private Activity activity;
    private Method realMethod;

    public EventInvocationHandler(Activity activity, Method realMethod) {
        this.activity = activity;
        this.realMethod = realMethod;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Log.i(TAG, "invoke: 开始调用："+realMethod.getName()+",由 "+method.getName()+" 调起");
        Object invoke = null;
        try {
            invoke = realMethod.invoke(activity, args);
        Log.i(TAG, "invoke: 结束调用:"+realMethod.getName());
        } catch (Exception e) {
            Log.i(TAG, "invoke: 调用异常:"+realMethod.getName()+"-->"+e.getMessage());
            e.printStackTrace();
        }
        return invoke;
    }
}
